/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.repository;

import me.zhengjie.modules.system.domain.SaleWellProduct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @author 李忠臻
* @date 2024-02-29
**/
public interface SaleWellProductRepository extends JpaRepository<SaleWellProduct, Long>, JpaSpecificationExecutor<SaleWellProduct> {

    @Query(value = "select min(id) as id, model, ?1 as from_now_day, count(*) as purchase_time, sum(quantity) as quantity " +
                   "from purchase_data where DATEDIFF(NOW(), create_time) < ?1 group by model having count(*) >= ?2 ",
           countQuery = "select count(*) from (select min(id) as id, model, ?1 as from_now_day, count(*) as purchase_time " +
                   "from purchase_data where DATEDIFF(NOW(), create_time) < ?1  " +
                   "group by model having count(*) >= ?2 ) v ",
            nativeQuery = true)
    Page<SaleWellProduct> querySaleWellProduct(@Param("fromNowDay") Integer fromNowDay,
                                               @Param("purchaseTime") Integer purchaseTime,
                                               Pageable pageable);

    @Query(value = "select min(id) as id, model, ?1 as from_now_day, count(*) as purchase_time, sum(quantity) as quantity " +
                   "from purchase_data where DATEDIFF(NOW(), create_time) < ?1 group by model having count(*) >= ?2 ",
            nativeQuery = true)
    List<SaleWellProduct> querySaleWellProduct(@Param("fromNowDay") Integer fromNowDay,
                                               @Param("purchaseTime") Integer purchaseTime);

}